import ErrorComp from '@/components/common/error-comp';
import MessageBox from '@/components/common/message-box';
import SendMessage from '@/components/common/send-message';
import { getSession } from '@/lib/auth';
import { getMessages } from '@/service/query/message';
import { z } from 'zod';

export const metadata = {
  title: '在线客服',
};

const paramSchema = z.object({
  id: z.string(),
});

export default async function Page({
  params,
}: { params: Promise<{ id: string }> }) {
  const paramsRes = paramSchema.safeParse(await params);
  if (!paramsRes.success)
    return <ErrorComp message="参数错误" backPath="/message" />;

  const session = await getSession();
  if (!session?.user) return <ErrorComp message="用户未登录" />;

  const { result, error } = await getMessages({
    userId: session.user.id,
    shopUserId: paramsRes.data.id,
  });
  if (error) return <ErrorComp message={error.message} backPath="/message" />;

  return (
    <div className="space-y-12 pb-10 px-16 pt-20 min-h-screen shadow-lg relative">
      <MessageBox initMessages={result} toId={paramsRes.data.id} />
    </div>
  );
}
